package com.cartview.action;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.cartitem.bean.CartItem;
import com.cartitem.service.ICartItemService;
import com.cartview.service.ICartViewService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@Controller("cartViewAction")
@Scope("prototype")
public class CartViewAction extends ActionSupport {

	public void setCartItemService(ICartItemService cartItemService) {
		this.cartItemService = cartItemService;
	}

	public String getBookid() {
		return bookid;
	}

	public void setBookid(String bookid) {
		this.bookid = bookid;
	}

	public void setCartViewService(ICartViewService cartViewService) {
		this.cartViewService = cartViewService;
	}
	
	public String getCartView() {
		ActionContext.getContext().getSession().remove("cartview");
		String username = (String) ActionContext.getContext().getSession().get("user");
		ActionContext.getContext().getSession().put("cartview", cartViewService.getCartByUsername(username));
		return SUCCESS;
	}
	
	public String deleteBookById() {
		String username = (String) ActionContext.getContext().getSession().get("user");
		CartItem cartItem = cartItemService.getCartByUsernameAndBookid(username, bookid);
		cartItemService.deleteCartItem(cartItem);
		getCartView();
		
		return SUCCESS;
	}

	private static final long serialVersionUID = 726915325472537056L;

	@Autowired
	private ICartViewService cartViewService;
	
	@Autowired
	private ICartItemService cartItemService;
	
	private String bookid;
}
